package com.lyk.swap;

import java.util.ArrayList;
import java.util.List;

public class MonthPrice extends ShowPrice {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7546981213041460096L;
	private int year;
	private int month;

	public MonthPrice() {
		super();
	}

	public MonthPrice(int year, int month) {
		super();
		this.year = year;
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	@Override
	public String getFormatDate() {
		StringBuilder sb = new StringBuilder();
		sb.append(month + 1);
		sb.append('/');
		sb.append(year + 1900);
		return sb.toString();
	}

	@SuppressWarnings("deprecation")
	public static List<MonthPrice> createMonthPriceList(List<DatePrice> dplist) {
		List<MonthPrice> mplist = new ArrayList<MonthPrice>();
		int y = -1, m = -1;
		double profit = 0;
		MonthPrice mp = null;
		for (DatePrice dp : dplist) {
			int dpy = dp.getDate().getYear(), dpm = dp.getDate().getMonth();
			if (y != dpy || m != dpm) {
				if (null != mp) {
					mp.setPrice(dp.getPrice());
					mp.setProfit(profit);
					mplist.add(mp);
				}
				y = dpy;
				m = dpm;
				mp = new MonthPrice(y, m);
				profit = 0;
			}
			profit += dp.getProfit();
		}
		if (null != mp) {
			mp.setPrice(dplist.get(dplist.size() - 1).getPrice());
			mp.setProfit(profit);
			mplist.add(mp);
		}
		return mplist;
	}
}
